﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
//using Bzure.Apps.Security.ResourceFiles;
using Bzure.Apps.Security.ViewModels;
using SharpDom.Mvc;
using Bzure.Mvc.Controllers;

namespace Bzure.Apps.Security.Controllers
{
    [MvcControllerFilter("_bzure_filted_controller")]
    public abstract class T4ViewController : Controller
    {

        public ActionResult Result<T>(T ViewModel) where T : IPageViewModel
        {
            IViewBuilder handler = default(IViewBuilder);
            if (!ApplicationStartContextSecurityMVCExtensions.pageViews.TryGetValue(typeof(T), out handler))
            {
                return Content("没有找到跟视图模型对应的视图");
            }
            ViewBuilder<T> builder = handler as ViewBuilder<T>;
            SharpDomView<T> page = builder.PageBuilder(ViewModel);
            if (page is PageView<T>)
            {
                PageView<T> pv = page as PageView<T>;
                pv.MasterBuilder = builder.MasterBuilder;
            }
            else
            {
                this.ClearBzureFlag();
            }
            return View(page, ViewModel);
        }
        protected void ClearBzureFlag()
        {
            if (this.HttpContext.Items.Contains(MvcControllerFilter.BzureControllerKey))
                this.HttpContext.Items.Remove(MvcControllerFilter.BzureControllerKey);
        }
    }
}
